#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>

#define PROC_ARRAY_NUM        20
#define PROC_FILE_PATH        "/proc/tc3162/ethertypefilter_array"

int get_dig_ip(void)
{
	const char *url = "qq.com";
	char *pend = NULL;
	FILE *fp = NULL;
	int answer_section = 0;
	char buf[128] = {0};

	snprintf(buf, sizeof(buf), "/userfs/bin/dig -c 1 -t A -i 5000 %s", url);
	fp = popen(buf, "r");
	if (!fp)
	{
		perror("popen");
		return -1;
	}

	memset(buf, 0, sizeof(buf));
	while(fgets(buf, sizeof(buf), fp))
	{
		if (!answer_section && strstr(buf, "ANSWER SECTION"))
		{
			answer_section = 1;
			continue;
		}

		if (answer_section && strstr(buf, "AUTHORITY SECTION"))
			break;

		if (answer_section)
		{
			pend = strrchr(buf, '\n');
			if (pend)
				*pend = '\0';
			if ((pend = strrchr(buf, 'A')))
			{
				printf("ip:%s===.\n", pend + 2);
			}
		}
		memset(buf, 0, sizeof(buf));
	}

	return 0;
}

int main(int argc, char **argv)
{
    int i = 0;
    int fd = -1;
    char *prot[PROC_ARRAY_NUM] = {"IPv4oE", "PPPoE", "ARP", "IPv6oE", ""};
    char buff[128] = {0};
#if 0
    fd = open(PROC_FILE_PATH, O_NONBLOCK|O_WRONLY);
    if (fd < 0)
    {
        printf("open fail:%d.\n", errno);
        return -1;
    }

    for (i = 0; i < PROC_ARRAY_NUM; ++i)
    {
        snprintf(buff, sizeof(buff), "%d %s", i, prot[i]);
        if (write(fd, buff, strlen(buff)) < 0)
            printf("write fail:%d:%s.\n", errno, buff);
    }

    close(fd);
    const char format[] = {"\"status:\":%s"
    "\"receive\":%llu"
    "\"send\":%llu"
    "\"receivedata\":%llu"
    "\"senddata\":%llu"};

    snprintf(buff, sizeof(buff), format, "request", 0ULL, 0ULL, 0ULL, 0ULL);
    printf("format:%s.\n", buff);
#endif

	printf("atoi(No):%d.\n", atoi("No"));
	get_dig_ip();

	long ld = 12345678;
	printf("ld pointer:%p.\n", ld);
	printf("ld pointer:%p.\n", &ld);

    return 0;
}
